//Page Object
const app = getApp()
Page({
  data: {
    heng: false,
    showForm: false,
    showWord: true,
    showTimeUp: true,
    name: '',
    time: 120,
    current: '你划我猜出题器',
    len: 0,
    correct: 0
  },
  blurName(e) {
    this.setData({
      name: e.detail.value
    })
  },
  blurTime(e) {
    let t = e.detail.value
    if(t <= 0 || !t) t = 120
    this.setData({
      time: parseInt(t)
    })
  },
  startTick() {
    let that = this
    let tick = setInterval(function() {
      let t = that.data.time - 1
      if(t < 0) {
        clearInterval(tick)
        that.endGame()
      } else {
        that.setData({
          time: t
        })
      }
    }, 1000)
  },
  startGame() {
    this.setData({
      showForm: true,
      showWord: false
    })
    this.startTick()
  }, 
  nextWord() {
    app.globalData.word.nextWord()
    this.setData({
      current: app.globalData.word.getCurrentWord()
    })
  },
  correctWord() {
    if(this.data.current != '题库已无题目') {
      app.globalData.word.correctWord()
      this.setData({
        correct: this.data.correct + 1,
        current: app.globalData.word.getCurrentWord()
      })
    }
  },
  endGame() { // 时间到
    this.setData({
      showWord: true,
      showTimeUp: false
    })
    let date = new Date()
    let t = `${(date.getMonth()+1)>=10?date.getMonth()+1:'0'+(date.getMonth()+1)}-${date.getDate()>=10?date.getDate():'0'+date.getDate()} ${date.getHours() > 10?date.getHours():'0'+date.getHours()}:${date.getMinutes()>=10?date.getMinutes():'0'+date.getMinutes()}`
    let scores = wx.getStorageSync('scores')
    if(scores) {
      scores.push({name: this.data.name, score: this.data.correct, date: t})
      wx.setStorageSync('scores', scores)
    } else {
      let arr = [{name: this.data.name, score: this.data.correct, date: t}]
      wx.setStorageSync('scores', arr)
    }
  },
  onLoad(e) {
    let num = 0
    if(e.type) {
      num = parseInt(e.type)
    }
    // if(e.type == undefined || e.type == null) e.type = 0
    app.globalData.word.setTikuNum(num)
    app.globalData.word.setTimuNum(0)
    app.globalData.word.randomTiku()
    this.setData({
      current: app.globalData.word.getCurrentWord(),
      len: app.globalData.word.getCurrentTikuLen()
    })
  },
  onResize:function(res) {

    console.log(res.size.windowWidth, res.size.windowHeight)
    let w = res.size.windowWidth
    let h = res.size.windowHeight
    if(w > h) {
      this.setData({
        heng: true
      })
    } else {
      this.setData({
        heng: false
      })
    }
    console.log(this.data.heng?'横屏':'竖屏')
    // res.size.windowWidth // 新的显示区域宽度
    // res.size.windowHeight // 新的显示区域高度
  }
});